// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <iostream>

#include <pollux/vector/flat_vector.h>
#include <pollux/vector/fuzzer/generator_spec.h>
#include <tests/vector/fuzzer/examples/Utils.h>

using namespace kumo::pollux;
using namespace generator_spec_maker;
using namespace generator_spec_examples;

int main() {
  // This example shows how to use our EncoderSpec class to generate vectors
  // of scalars with user defined encoding distributions.

  constexpr size_t numIters = 1000;
  constexpr int sampleSize = 100;
  constexpr double mu = 5.0, sigma = 2.0;
  constexpr double nullProbability = 0.67;

  auto normal = std::normal_distribution<double>(mu, sigma);

  auto realGenerator = std::uniform_real_distribution<>(0.0, 1.0);
  auto threshold = 0.367;
  auto encoding = [&](FuzzerGenerator& rng) {
    auto val = realGenerator(rng);
    if (val <= threshold) {
      return GeneratorSpec::EncoderSpecCodes::CONSTANT;
    } else {
      return GeneratorSpec::EncoderSpecCodes::DICTIONARY;
    }
  };

  FuzzerGenerator rng;
  memory::MemoryManager::initialize({});
  auto pool = memory::memoryManager()->addLeafPool();
  GeneratorSpecPtr encodedNormalDoubleGenerator =
      ENCODE(RANDOM_DOUBLE(normal, nullProbability), encoding, 1, 5, 0.3);

  int32_t numConst = 0, numDict = 0;
  for (size_t i = 0; i < numIters; ++i) {
    auto vec =
        encodedNormalDoubleGenerator->generateData(rng, pool.get(), sampleSize);
    if (vec->encoding() == VectorEncoding::Simple::CONSTANT) {
      ++numConst;
    } else if (vec->encoding() == VectorEncoding::Simple::DICTIONARY) {
      ++numDict;
    } else {
      std::cout << "Unxpected encoding!!!\n";
      return 0;
    }
  }

  std::cout << "Probability of constant encoding = "
            << static_cast<double>(numConst) /
          (static_cast<double>(numConst) + static_cast<double>(numDict))
            << "\n";

  return 0;
}
